import {
  CallHandler,
  ExecutionContext,
  Injectable,
  NestInterceptor,
} from '@nestjs/common';
import { map, Observable } from 'rxjs';

interface Data<T> {
  data: T;
  code: number;
  msg: string;
}

@Injectable()
export class TestresponseInterceptor<T> implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<Data<T>> {
    return next
      .handle()
      .pipe(
        map(({ data, msg = '请求成功', code = 200 }) => ({ code, msg, data })),
      );
  }
}
